package com.ym521.facecheck

import android.content.Context
import android.graphics.Bitmap
import androidx.camera.core.ImageProxy
import com.ym521.facecheck.bean.ImageHandlerType
import com.ym521.facecheck.callback.FaceCheckCallback
import com.ym521.facecheck.core.IFaceCheck
import com.ym521.facecheck.core.IImageQueueEngine
import com.ym521.facecheck.core.ImageQueueEngine
import com.ym521.facecheck.core._FaceCheck
import com.ym521.facecheck.utils.LogFC

/**
 *@author Ym
 *E-mail: 2435970206@qq.com
 *createTime:2024/6/9
 *explain: 人脸检测
 */
object FaceCheck {
    private const val TAG = "FaceCheck"

    private var faceCheckEngin: IFaceCheck? = null
    private var imageQueueEngine: IImageQueueEngine? = null

    fun install(mContext: Context): Setting {
        LogFC.d(TAG, "install()")
        if (faceCheckEngin == null) {
            val faceCheckEngin = _FaceCheck
                .build()
                .initSDK(mContext)
            this.faceCheckEngin = faceCheckEngin

            imageQueueEngine = ImageQueueEngine
                .build()
                .initQueueEngine(faceCheckEngin)
                .startRunning()
        }
        return Setting()
    }

    class Setting {
        /**
         * 是否开启记忆功能
         * 记忆功能开启 可以减少CPU的使用率
         */
        fun memoryEnabled(enabled: Boolean): Setting {
            LogFC.d(TAG, "memoryEnabled($enabled)")
            _FaceCheck.memoryEnabled = enabled
            return this
        }

        /**
         * 是否开启检测的可视化图像输出
         * 检测的可视化图像输出开启 会增加 CPU的使用率
         */
        fun visualizeEnabled(enabled: Boolean): Setting {
            LogFC.d(TAG, "visualizeEnabled($enabled)")
            _FaceCheck.visualizeEnabled = enabled
            return this
        }

        /**
         * 检测结果的输出监听
         */
        fun faceCheckListener(callback: FaceCheckCallback): Setting {
            LogFC.d(TAG, "faceCheckListener()")
            faceCheckEngin?.setFaceCheckCallback(callback)
            return this
        }

        /**
         * 日志是否开启
         */
        fun logEnabled(): Setting {
            LogFC.d(TAG, "logEnabled()")
            LogFC.enabled = true
            return this
        }

    }

    /**
     * 检测图像帧 请求函数
     * 算法更优
     */
    fun faceCheck(image: Bitmap) {
        imageQueueEngine?.putImage(image)
    }

    /**
     * 检测图像帧 请求函数
     */
    fun faceCheck(image: ImageProxy) {
        imageQueueEngine?.putImage(image)
    }

    /**
     * 不需要人脸检测功能
     * 释放资源
     */
    fun release() {
        LogFC.d(TAG, "release()")
        imageQueueEngine?.release()
        imageQueueEngine = null
        faceCheckEngin?.release()
        faceCheckEngin = null
    }

}